package com.aischool.api;

import com.aischool.common.model.AiArticle;
import com.aischool.params.AiArticleParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiArticleService;
import com.aischool.service.AiArticlecatService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.StateEnum;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/api/article")
public class AiArticleController extends Controller {

	@Inject
	AiArticleService aiArticleService;
	@Inject
	AiArticlecatService aiArticlecatService;
	
	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiArticleParams params = new AiArticleParams();
		// 获取参数
		int initno = getInt("initno", 0);
		int type_id = getParaToInt("type_id",0);
		
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
//		if (getParaToInt("state")!=null) {
//			params.setState(getParaToInt("state"));
//		}
		params.setState(getInt("state", StateEnum.ALL.getState()));
		if (StrKit.notBlank(get("keywords",""))) {
			params.setKeywords(get("keywords",""));
		}
		params.setCatId(getInt("cat_id",0));
		params.setTypeId(getInt("type_id",0));
		
		if (initno == 1 && type_id > 0) {
			Object obj = aiArticlecatService.getAll(type_id, 1);
			if (obj != null) { jr.setDicKV("listcat", obj); }
		}
		// 获取分页
		Object pageData = aiArticleService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	/**
	 * 获取单个信息
	 */
	public void getOne() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 获取单个用户信息
		int article_id = getParaToInt("id",0);
		int type_id = getParaToInt("type_id",0);
		
		Object obj = aiArticlecatService.getAll(type_id, 1);
		if (obj != null) { jr.setDicKV("listcat", obj); }
		
		if (article_id > 0) {
			obj = aiArticleService.getOne(article_id);
			if (obj != null) { jr.setDicKV("oneinfo", obj); }
		}

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int article_id = getParaToInt("article_id", 0);
		AiArticle info = getModel(AiArticle.class, "", true);
		
		if (StrKit.isBlank(info.getArticleTitle())) {
			jr.appendMsg("请输入标题！");
		}else if (StrKit.isBlank(info.getArticleDesc())) {
			jr.appendMsg("请输入内容！");
		}else if (getParaToInt("cat_id", 0)<=0) {
			jr.appendMsg("请选择栏目！");
		}else {
			
			info.setUpdateTime(DateUtil.getTimestamp());
			
			info.removeNullValueAttrs();
			
			boolean bf = false;
			if (article_id >0) {
				// 更新
				bf = info.update();
			}else {
				// 插入
				info.setAddTime(DateUtil.getTimestamp());
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		boolean bf = false;
		// 表单转换类
		int article_id = getParaToInt("id", 0);
		if (article_id > 0) {
			bf = aiArticleService.del(article_id);
		}else {
			String ids = get("ids","");
			bf = aiArticleService.delMore(ids);
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
}
